

package com.io.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.io.constant.Constant;
import com.io.dao.SysRoleDao;
import com.io.dao.SysUserDao;
import com.io.entity.SysDeptEntity;
import com.io.entity.SysRoleEntity;
import com.io.service.*;
import com.io.utils.PageUtils;
import com.io.utils.Query;
import com.io.utils.shiro.ShiroUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


/**
 * 角色
 *
 * @author guodd
 */
@Service("sysRoleService")
public class SysRoleServiceImpl extends ServiceImpl<SysRoleDao, SysRoleEntity> implements SysRoleService {
    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @Autowired
    private SysRoleDeptService sysRoleDeptService;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private SysDeptService sysDeptService;

    @Autowired
    private SysRoleDao sysRoleDao;

    @Autowired
    private SysUserDao sysUserDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String roleName = (String) params.get("roleName");

        IPage<SysRoleEntity> page = this.page(
                new Query<SysRoleEntity>().getPage(params),
                new QueryWrapper<SysRoleEntity>()
                        .like(StringUtils.isNotBlank(roleName), "role_name", roleName)
                        .apply(params.get(Constant.SQL_FILTER) != null, (String) params.get(Constant.SQL_FILTER))
        );

        for (SysRoleEntity sysRoleEntity : page.getRecords()) {
            SysDeptEntity sysDeptEntity = sysDeptService.getById(sysRoleEntity.getDeptId());
            if (sysDeptEntity != null) {
                sysRoleEntity.setDeptName(sysDeptEntity.getName());
            }
        }

        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRole(SysRoleEntity role) {
        role.setCreateTime(new Date());
        this.save(role);

        //保存角色与菜单关系
        sysRoleMenuService.saveOrUpdate(role.getId(), role.getMenuIdList());

        //保存角色与部门关系
        sysRoleDeptService.saveOrUpdate(role.getId(), role.getDeptIdList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysRoleEntity role) {
        this.updateById(role);

        //更新角色与菜单关系
        sysRoleMenuService.saveOrUpdate(role.getId(), role.getMenuIdList());

        //保存角色与部门关系
        sysRoleDeptService.saveOrUpdate(role.getId(), role.getDeptIdList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] roleIds) {
        if (roleIds.length > 0) {
            //删除角色
            this.removeByIds(Arrays.asList(roleIds));

            //删除角色与菜单关联
            sysRoleMenuService.deleteBatch(roleIds);

            //删除角色与部门关联
            sysRoleDeptService.deleteBatch(roleIds);

            //删除角色与用户关联
            sysUserRoleService.deleteBatch(roleIds);
        }
    }

    @Override
    public Set<String> findRoleByUsername(String username) {
        Set<String> roles;
        // 超级管理员
        if (ShiroUtils.getUserId() == 1) {
            List<SysRoleEntity> roleList = sysRoleDao.selectByMap(new HashMap<>());
            roles = new HashSet<>(roleList.size());
            for (SysRoleEntity role : roleList) {
                roles.add(role.getId().toString());
            }
        } else {
            // 非超管用户
            roles = sysRoleDao.queryAllRoles(ShiroUtils.getUserId());
        }
        return roles;
    }

}
